using System;
using System.Collections.Generic;
using System.Linq;
using WeChatCmsCommon.EnumBusiness;
using WeChatDataAccess;
using WeChatModel.DatabaseModel;

namespace WeChatService
{
    /// <summary>
    /// 人员标签服务
    /// </summary>
    public class PersonTagService
    {
        /// <summary>
        /// 数据服务
        /// </summary>
        private PersonTagData _dataAccess = new PersonTagData();
        /// <summary>
        /// 根据人员ID获取标签列表
        /// </summary>
        /// <param name="personId">人员ID</param>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public List<PersonTagModel> GetListByPersonId(long personId, long userId)
        {
            return _dataAccess.GetListByPersonId(personId, userId);
        }

        /// <summary>
        /// 根据ID获取标签
        /// </summary>
        /// <param name="id">标签ID</param>
        /// <returns></returns>
        public PersonTagModel Get(long id)
        {
            if (id < 1) return null;
            var data = _dataAccess.Get(id);
            if (data == null || data.IsDel == FlagEnum.HadOne) return null;
            return data;
        }

        /// <summary>
        /// 添加标签
        /// </summary>
        /// <param name="model">标签模型</param>
        /// <returns>标签ID</returns>
        public long Add(PersonTagModel model)
        {
            if (model == null) return 0;
            model.Id = 0; // 确保是添加操作
            return _dataAccess.SaveModel(model);
        }

        /// <summary>
        /// 更新标签
        /// </summary>
        /// <param name="model">标签模型</param>
        /// <returns></returns>
        public bool Update(PersonTagModel model)
        {
            if (model == null || model.Id < 1) return false;
            var result = _dataAccess.SaveModel(model);
            return result > 0;
        }

        /// <summary>
        /// 检查标签是否存在
        /// </summary>
        /// <param name="personId">人员ID</param>
        /// <param name="tagName">标签名称</param>
        /// <param name="userId">用户ID</param>
        /// <param name="excludeId">排除的标签ID（编辑时使用）</param>
        /// <returns></returns>
        public bool IsTagExists(long personId, string tagName, long userId, long? excludeId = null)
        {
            return _dataAccess.IsTagExists(personId, tagName, userId, excludeId);
        }

        /// <summary>
        /// 删除标签
        /// </summary>
        /// <param name="id">标签ID</param>
        public void DelModel(long id)
        {
            _dataAccess.DelModel(id);
        }

        /// <summary>
        /// 保存标签信息
        /// </summary>
        /// <param name="model">标签模型</param>
        /// <returns></returns>
        public long SaveModel(PersonTagModel model)
        {
            if (model == null) return 0;
            return _dataAccess.SaveModel(model);
        }
    }
}